<?php

namespace Backend\Controllers;

use Models\Act;
use Models\ActUser;
use Models\Wx;

class AppactController extends BaseController
{
    public function beforeExecuteRoute($dispatcher)
    {
        parent::beforeExecuteRoute($dispatcher);
        $Cuser = $this->Cuser;
        $wxc = Wx::findFirstByuid($Cuser->uid);
        if (!$wxc) {
            $this->flash->warning("<a href='admin/config/wx'>请先点击这里进行微信配置，公共配置->微信配置</a>");
        }
        $this->view->setVar("wxc", $wxc->uid);
    }

    public function dashboardAction()
    {

    }
    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page');
        $Wx = Wx::findFirstByUid($this->Cuser->uid);
        $token = $Wx->token;
        $acts = Act::findByToken($token);
        $actsArr=$acts->toArray();
        for($i=0;$i<count($actsArr);++$i)
        {
            //-----报名人数统计--------//
            $count=ActUser::count("aid=".$actsArr[$i]['aid']);
            $actsArr[$i]['count']=$count;
        }
        $paginator = new \Phalcon\Paginator\Adapter\NativeArray(array(
            "data" => $actsArr,
            "limit" => 10,
            "page" => $currentPage,
        ));
        $this->view->page = $paginator->getPaginate();

        $host = $this->config->host;
        $actListUrl = $host . '/frontend/act/index' . '?token=' . $token;
        $this->view->setVar('actListUrl', $actListUrl);
    }

    public function detailAction($aid)
    {
        $act = Act::findFirstByAid($aid);
        if(!$act) return $this->flash->warning("不存在此活动");
        $actuser = ActUser::findByAid($aid);
        if($this->request->isPost()==true){
            $act->name = $this->request->getPost("name");
            $pic=$this->request->getPost("pic");
            if($pic!=null){  $act->pic = $this->request->getPost("pic");}
            $act->time = $this->request->getPost("time");
            $act->place = $this->request->getPost("place");
            $act->detail = $this->request->getPost('editorValue');
            if ($act->save()) {

                return $this->response->redirect("admin/appact");
            } else {
                return $this->flash->warning("编辑失败");
            }
        }
        else{
            $this->view->setVar('act', $act);
            $this->view->setVar('actuser', $actuser);
        }

    }

    public function manageAction($aid)
    {
        $act = Act::findFirstByAid($aid);
        $this->view->setVar('act', $act);
        $currentPage = $this->request->getQuery('page');
        $actusers = ActUser::findByAid($aid);
        $paginator = new \Phalcon\Paginator\Adapter\Model(array(
            "data" => $actusers,
            "limit" => 20,
            "page" => $currentPage,
        ));
        $this->view->page = $paginator->getPaginate();

        if($post=$this->request->getPost())
        {
            if(!$post['name']||!$post['tel']) return $this->flash->warning("请输入完整");
            //print_r($post);
            else
            {
                $actuser=new ActUser();
                $actuser->name=$post['name'];
                $actuser->tel=$post['tel'];
                $actuser->time=time();
                $actuser->aid=$aid;

                $Wx = Wx::findFirstByUid($this->Cuser->uid);
                $token = $Wx->token;
                $actuser->token=$token;
                $actuser->create();
                return $this->response->redirect("admin/appact/manage/".$aid);

            }
        }


    }

    public function newactAction()
    {
        if ($post=$this->request->getPost()) {
            //print_r($post);
            $name = $this->request->getPost("name");
            $pic = $this->request->getPost("pic");
            $time = $this->request->getPost("time");
            //------日期比较------//
            if(date($time)<date("Y-m-d")) return $this->flash->warning("日期已过期,请重新填写");
            $place = $this->request->getPost("place");
            $detail = $this->request->getPost('editorValue');
            $Wx = Wx::findFirstByUid($this->Cuser->uid);
            $token = $Wx->token;

            if (!$name || !$time || !$place || !$detail) {
                return $this->flash->warning("请输入完整");
            }

            $act = Act::findFirst("name='".$name."' AND token='".$token."'");
            if (!$act) {
                $act = new Act();
                $act->name = $name;
                $act->pic = $pic;
                $act->createtime=date("Y-m-d H:i:s", strtotime("now"));
                $act->time = $time;
                $act->place = $place;
                $act->token = $token;
                //$act->detail=$detail;
                $act->detail = $detail;
                $act->create();

                $host = $this->config->host;
                $acturl = $host . '/frontend/act/detail' . '?id=' . $act->aid . '&token=' . $token;
                $this->view->setVar('acturl', $acturl);
                $act->url = $acturl;
                $act->save();
                return $this->flash->warning("创建成功");
            } else {
                return $this->flash->warning("活动已存在，请修改或删除");
            }
        }
    }

    public function deleteAction($aid)
    {

        $this->view->disable();
        $act = Act::findFirstByAid(intval($aid));
        if ($act) {
            $act->delete();
            $status = 'success';
        } else {
            $status = 'error';
        }
        return json_encode(array('status' => $status));

    }
    public function deleteuserAction($id)
    {
        $this->view->disable();
        $actuser=ActUser::findFirstById("$id");
        if($actuser){
            $actuser->delete();
            $status='success';
        }
        else{
            $status='error';
        }
        return json_encode(array('status'=>$status));
    }




}